//
//  CalendarMarkService.m
//  YouthOlympic2014
//
//  Created by Li Yuhang on 14-7-2.
//  Copyright (c) 2014年 mitian. All rights reserved.
//

#import "CalendarMarkService.h"
#import "CalendarMark.h"
@implementation CalendarMarkService



-(void) getDaysByGameType:(NSString*) gameTypeId
           andGameSubType:(NSString*) gameSubTypeId
               completion:(CompletionBlock)completion
{
    //处理超时
    __block BOOL isResultCompletion = NO;
    dispatch_time_t popTime = dispatch_time(DISPATCH_TIME_NOW, RESULT_TIME_MAX * NSEC_PER_SEC);
    dispatch_after(popTime,dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^(void){
        if (!isResultCompletion) {
            isResultCompletion = YES;
            NSString* errorMsg = @"timeout";
            NSError* error = [NSError errorWithDomain:errorMsg code:504 userInfo:nil];
            completion(nil,error);
        }
    });
    
    dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
        NSError* error = nil;
        NSArray* result = nil;
        id jsonObj =  [self.api GetDateMarksByType:gameTypeId
                                        andSubType:gameSubTypeId
                                           withLan:[AppUtils getLan]];
        
        NSString* errorMsg = [[jsonObj objectForKey:@"ApiErrorInfo"] objectForKey:@"msg"];
        
        if (errorMsg != nil) {
            error = [NSError errorWithDomain:errorMsg code:400 userInfo:nil];
        }else{
            result =[CalendarMark getDays:jsonObj];
        }
        NSLog(@"getDaysByGameType result is %@",[CalendarMark getDays:jsonObj]);
        
        dispatch_async(dispatch_get_main_queue(), ^{
            if (!isResultCompletion) {
                isResultCompletion = YES;
                completion(result,error);
            }
        });
    });
}


-(void) getDaysByPlaceType:(NSString*) placeTypeId
           andSubPlaceType:(NSString*) subPlaceTypeId
                completion:(CompletionBlock)completion
{
    //处理超时
    __block BOOL isResultCompletion = NO;
    dispatch_time_t popTime = dispatch_time(DISPATCH_TIME_NOW, RESULT_TIME_MAX * NSEC_PER_SEC);
    dispatch_after(popTime,dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^(void){
        if (!isResultCompletion) {
            isResultCompletion = YES;
            NSString* errorMsg = @"timeout";
            NSError* error = [NSError errorWithDomain:errorMsg code:504 userInfo:nil];
            completion(nil,error);
        }
    });
    
    dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
        NSError* error = nil;
        NSArray* result = nil;
        id jsonObj =  [self.api GetDateMarksByPlace:placeTypeId
                                        andSubPlace:subPlaceTypeId
                                            withLan:[AppUtils getLan]];
        
        NSString* errorMsg = [[jsonObj objectForKey:@"ApiErrorInfo"] objectForKey:@"msg"];
        
        if (errorMsg != nil) {
            error = [NSError errorWithDomain:errorMsg code:400 userInfo:nil];
        }else{
            result =[CalendarMark getDays:jsonObj];
        }
        NSLog(@"getDaysByGameType result is %@",[CalendarMark getDays:jsonObj]);
        
        dispatch_async(dispatch_get_main_queue(), ^{
            if (!isResultCompletion) {
                isResultCompletion = YES;
                completion(result,error);
            }
        });
    });
}




-(NSArray*) getDaysBySchedule{
    FMDatabase* db = [DBUtil getDatabase];
    
    FMResultSet* rs = [db executeQuery:@"select day, count(1) num from schedule group by day"];
    NSMutableArray* days = [[NSMutableArray alloc] init];
    if([rs next]){
        [days addObject: [NSNumber numberWithInt: [rs intForColumn:@"day"]]];
    }
    
    [rs close];
    [db close];
    return [NSArray arrayWithArray:days];
}


/*
 -(NSArray*) getDaysByGameType:(NSString*) gameTypeId
 andGameSubType:(NSString*) gameSubTypeId{
 id jsonObj = [self.api GetDateMarksByType:gameTypeId
 andSubType:gameSubTypeId
 withLan:[AppUtils getLan]];
 NSLogger(@"getDaysByGameType result is %@",[CalendarMark getDays:jsonObj]);
 
 return [CalendarMark getDays:jsonObj];
 }
 
 
 -(NSArray*) getDaysByPlaceType:(NSString*) placeTypeId
 andSubPlaceType:(NSString*) subPlaceTypeId{
 id jsonObj = [self.api GetDateMarksByPlace:placeTypeId
 andSubPlace:subPlaceTypeId
 withLan:[AppUtils getLan]];
 
 NSLogger(@"getDaysByPlaceType result is %@",[CalendarMark getDays:jsonObj]);
 
 return [CalendarMark getDays:jsonObj];
 }

 */

@end
